package stark.app.function.teleprompter;

import android.os.Build;

import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.teleprompter.lib.TpFloatService;
import com.stark.teleprompter.lib.TpSettingManager;
import com.stark.teleprompter.lib.db.TaiciBean;
import com.stark.teleprompter.lib.db.TaiciDbHelper;

import java.util.List;

import androidx.lifecycle.Observer;
import stark.app.R;
import stark.app.databinding.ActivityTeleprompterDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.FastClickUtil;

/**
 * Author：daizhongbin on 2022/1/7
 * Email：1027154338@qq.com
 * Desc:
 */
public class TeleprompterDemoActivity extends BaseNoModelActivity<ActivityTeleprompterDemoBinding> {

    private List<TaiciBean> mTaiciBeanList;

    @Override
    protected int onCreate() {
        return R.layout.activity_teleprompter_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnAddTaici.setOnClickListener(v -> {
            addTaici();
        });
        mDataBinding.btnStart.setOnClickListener(v -> {
            reqPermissionAndStart();
        });
        mDataBinding.btnStop.setOnClickListener(v -> {
            TpFloatService.stop(this);
        });
        TaiciDbHelper.getTaiciBeans().observe(this, new Observer<List<TaiciBean>>() {
            @Override
            public void onChanged(List<TaiciBean> taiciBeans) {
                mTaiciBeanList = taiciBeans;
            }
        });
    }

    @Override
    protected void initData() {
    }

    private static final String CONTENT = "斯洛文尼亚总统会见了中国驻斯大使，并请大使代为转交信函。其同时表示，对疫情结束后访问中国一事，十分期待。斯洛文尼亚有着210万人口，在冰雪运动上表现出彩，运动员曾多次在冬奥赛场上摘金夺银。而在不久前，斯洛文尼亚举行了一项颇有意义的活动，斯方将阿尔卑斯山脉的冰块以卡车运载，途径意大利、德国、俄罗斯等国的冬奥举办城市，最终历经13000公里距离运抵北京，冰块预计将于北京冬奥会期间进行展示。";
    private void addTaici() {
        if (FastClickUtil.isFastClick()) return;

        TaiciBean bean = new TaiciBean();
        long createTime = System.currentTimeMillis();
        bean.setCreateTime(createTime);
        bean.setTitle("台测测试" + createTime);
        bean.setContent(CONTENT);
        TaiciDbHelper.insert(bean);
        ToastUtils.showShort("已增加");
    }

    private void reqPermissionAndStart() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionUtils.requestDrawOverlays(new PermissionUtils.SimpleCallback() {
                @Override
                public void onGranted() {
                    startFloatService();
                }

                @Override
                public void onDenied() {
                    ToastUtils.showShort("未授予权限");
                }
            });
        } else {
            startFloatService();
        }
    }

    private void startFloatService() {
        if (mTaiciBeanList == null || mTaiciBeanList.size() == 0) {
            ToastUtils.showShort("请先增加台词");
            return;
        }
        //这里一定要将选中的台词设置进去后，再启动悬浮窗服务
        TpSettingManager.getInstance().setSelTaiciBean(mTaiciBeanList.get(0));
        TpFloatService.start(TeleprompterDemoActivity.this);
    }
}
